# Logistic regression

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

tf.set_random_seed(777)

# datasets
xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

# placeholder
X = tf.placeholder(tf.float32, shape=[None, 8])
Y = tf.placeholder(tf.float32, shape=[None, 1])

# model
W = tf.Variable(tf.random_normal([8, 1]), name="Weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

z = tf.matmul(X, W ) + b
a = tf.sigmoid(z)

# cost
cost = -tf.reduce_mean(Y * tf.log(a) + (1 - Y) * tf.log(1 - a))
cost_history = []

# Grandient Descent
dz = a - Y
dW = tf.matmul(tf.transpose(X), dz) / tf.cast(tf.shape(X)[0], tf.float32)
db = tf.reduce_mean(dz, axis=[0])

# update
learning_rate = 10e-2
update = [
    tf.assign(W, W - learning_rate * dW),
    tf.assign(b, b - learning_rate * db)
]

# accuracy
predicted = tf.cast(a > 0.5, tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), tf.float32))

# launch a session
sess = tf.Session()
sess.run(tf.global_variables_initializer())

# train model
for step in range(10001):
    cost_val, _ = sess.run([cost, update], feed_dict={X: x_data, Y: y_data})
    if step % 500 == 0:
        print("Step: ", step, "Cost: ", cost_val)
        cost_history.append(cost_val)

# plot
plt.plot(cost_history[1:])
plt.show()

# test reporter
acc_val = sess.run(accuracy, feed_dict={X: x_data, Y: y_data})
print("Accuracy: ", acc_val)
